<?php

namespace Modules\System\Http\Controllers;

use Yansongda\Pay\Log;
use Yansongda\Pay\Pay;
use App\Http\Controllers\Controller;


class PayController extends Controller
{
    protected $config = [
        'app_id' => 2018112862347372,
        'notify_url' => 'http://cheyoukong.1zhetejia.com/notify.php',
        'return_url' => 'http://cheyoukong.1zhetejia.com/return.php',
        'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl4lIAQ3WGLuvBkPBSTXFHnL/fgXxTI/qh/lFWzZ4oXXYYzojbkWf2das2WsipyIG+tYZbuL37FxK86Hm8JSfTkJseaKN7IajAaQX6JjG4XrycO1o0a33MmIpiMrl9ON2WJpjkSMKwen2zaM1XDEdFLlw95SEljjcCTQNzgHOryjq6Waw/WzGGg8C7zf0yKe75sfoU9km+V14T292NIvbprTOSpX+U1AnsTWTJCQQoVL3M2N5tYYltGe8CWJPRdh2jyJuBUjOoDDlsRnhsiGiSm0RX2yG9TD33X8md6LYmfdOJVVI0UIerO9tiXz5iMU7NmrIr0vUu1t2JK7Wz3haMQIDAQAB',
        // 加密方式： **RSA2**
        'private_key' => 'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCYGHk6D5yRcJ8QRA0icWz3EC2+P0qoWG9M1RjDw9pOuBf5lr/SjglOF1X9SLpWo5glczE24DISSvXiJSFab2dDTJ2+p3F+wl88wWmdGg1we9c7gvBAFWqpzhEcTJo6xsMt0aH4HUtvGlqPD164gWQVFzY7wtOezNTPf9xuOW8rM+aRPiLCV+Qt5Rwmlm67VsAihv+kyivA9YcSUup0xWHuz+srJL3tp2G14MTeOFN2Z28+HkUO4IUxIofKxujJhxAYZhQKXQjao+ZHPDuF8RlBKctiKTqJJNQ0bpjQoca5fSE7hfOxJTaESJilWpQjDCRO9lftgXnW0Mkm0XGP2ulPAgMBAAECggEAGgSdwEPpaavVWgf6Izk6Ba2b1Z6oG9I8/+y4NszrLGorl8Ok/tTTXKt7Rz/IuId1w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Uu2VtPDlIEHkagkY+WkMvKUKqmPdCKBRLZrD8AvuYw1q3zVirbUIF+BH9o2A+rG+8Pk1Uw==',
        'log' => [ // optional
            'file' => './logs/alipay.log',
            'level' => 'info', // 建议生产环境等级调整为 info，开发环境为 debug
            'type' => 'single', // optional, 可选 daily.
            'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
        ],
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
        ],
        'mode' => 'dev', // optional,设置此参数，将进入沙箱模式
    ];

    /*
     * @url http://cheyoukong.1zhetejia.com/System/PayController/index
     *
     */
    public function index(){
        $order = [
            'out_trade_no' => time(),
            'total_amount' => '1',
            'subject' => 'test subject - 测试',
        ];

        $alipay = Pay::alipay($this->config);

        return $alipay->web($order)->send(); // laravel 框架中请直接 return $alipay->app($order)
    }

    public function return(){
        $data = Pay::alipay($this->config)->verify(); // 是的，验签就这么简单！

        // 订单号：$data->out_trade_no
        // 支付宝交易号：$data->trade_no
        // 订单总金额：$data->total_amount
    }

    public function notify(){
        $alipay = Pay::alipay($this->config);

        try{
            $data = $alipay->verify(); // 是的，验签就这么简单！

            // 请自行对 trade_status 进行判断及其它逻辑进行判断，在支付宝的业务通知中，只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时，支付宝才会认定为买家付款成功。
            // 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号；
            // 2、判断total_amount是否确实为该订单的实际金额（即商户订单创建时的金额）；
            // 3、校验通知中的seller_id（或者seller_email) 是否为out_trade_no这笔单据的对应的操作方（有的时候，一个商户可能有多个seller_id/seller_email）；
            // 4、验证app_id是否为该商户本身。
            // 5、其它业务逻辑情况

            Log::debug('Alipay notify',$data->all());
        }catch(\Exception $e){
            // $e->getMessage();
        }

        return $alipay->success()->send();// laravel 框架中请直接 `return $alipay->success()`
    }
}
